package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 新建权限
     *
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /**
     * 查询权限列表
     *
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取查询信息
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //开启分页插件
        PageHelper.startPage(currentPage, pageSize);
        //判断是否有查询条件
        if (queryString != null && queryString.length() > 0) {

            queryString = "%" + queryString + "%";
        }
        //使用模糊查询
        Page<Permission> page = permissionDao.findPage(queryString);

        //返回查询结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 查询此id的权限信息
     *
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    /**
     * 修改权限
     *
     * @param permission
     * @return
     */
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        //查询是否有与角色表关联
        List<Role> roleList = permissionDao.findRoleById(id);
        //如果此权限有和角色关联
        if (roleList != null&&roleList.size()>0) {

         return false;
        }

        //删除权限
        permissionDao.delete(id);
        return true;
    }
}
